package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcDiscussComment;
import com.visizen.wisdomclass.server.model.WcDiscussCommentExample;

import java.util.List;

import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.model.dto.DiscussCommentExt;
import com.visizen.wisdomclass.server.model.dto.DiscussCommentReplyExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface WcDiscussCommentMapper {
    /**
     * 查询一条讨论的评论列表
     *
     * @param discussId
     * @param start
     * @param end
     * @return
     */
    @Select("SELECT\n" +
            "  *\n" +
            "FROM (\n" +
            "       SELECT\n" +
            "         d.*,\n" +
            "         u.area_id   AS userAreaId,\n" +
            "         u.attach_id AS userAttachId,\n" +
            "         u.fullname  AS userFullname\n" +
            "       FROM wc_discuss_comment AS d, wc_user AS u\n" +
            "       WHERE discuss_id = #{discussId} AND u.user_id = d.user_id order by comment_time asc) AS data " +
            " LIMIT #{start},#{end}")
    @Results({
            @Result(property = "discuss_comment_id", column = "discuss_comment_id"),
            @Result(property = "replyList", column = "discuss_comment_id", javaType = List.class, many = @Many(select = "select3ReplyByComment"))
    })
    List<DiscussCommentExt> selectCommentByDiscuss(@Param("discussId") Long discussId,
                                                   @Param("start") int start,
                                                   @Param("end") int end);

    /**
     * 获取某条评论的3条回复
     *
     * @param commentId
     * @return
     */
    @Select(" SELECT\n" +
            "  *\n" +
            " FROM (\n" +
            "  SELECT\n" +
            "    dcr.*,\n" +
            "    u.area_id  AS userAreaId,\n" +
            "    u.attach_id  AS userAttachId,\n" +
            "    u.fullname AS userFullname\n" +
            "  FROM wc_discuss_comment_reply AS dcr, wc_user AS u\n" +
            "  WHERE discuss_comment_id = #{commentId} AND u.user_id = dcr.user_id order by reply_time asc) as data" +
            " limit 3")
    @Results({
            @Result(property = "target_user_id", column = "target_user_id"),
            @Result(property = "targetUser", column = "target_user_id", one = @One(select = "selectUser"))
    })
    List<DiscussCommentReplyExt> select3ReplyByComment(@Param("commentId") Long commentId);

    /**
     * 获取某条评论的所有回复
     *
     * @param commentId
     * @return
     */
    @Select(" SELECT\n" +
            "  *\n" +
            " FROM (\n" +
            "  SELECT\n" +
            "    dcr.*,\n" +
            "    u.area_id  AS userAreaId,\n" +
            "    u.attach_id  AS userAttachId,\n" +
            "    u.fullname AS userFullname\n" +
            "  FROM wc_discuss_comment_reply AS dcr, wc_user AS u\n" +
            "  WHERE discuss_comment_id = #{commentId} AND u.user_id = dcr.user_id order by reply_time asc) as data")
    @Results({
            @Result(property = "target_user_id", column = "target_user_id"),
            @Result(property = "targetUser", column = "target_user_id", one = @One(select = "selectUser"))
    })
    List<DiscussCommentReplyExt> selectAllReplyByComment(@Param("commentId") Long commentId);

    /**
     * 获取某条评论的回复
     *
     * @param commentId
     * @return
     */
    @Select(" SELECT\n" +
            "  *\n" +
            " FROM (\n" +
            "  SELECT\n" +
            "    dcr.*,\n" +
            "    u.area_id  AS userAreaId,\n" +
            "    u.attach_id  AS userAttachId,\n" +
            "    u.fullname AS userFullname\n" +
            "  FROM wc_discuss_comment_reply AS dcr, wc_user AS u\n" +
            "  WHERE discuss_comment_id = #{commentId} AND u.user_id = dcr.user_id order by reply_time asc) as data" +
            " LIMIT #{start},#{end}")
    @Results({
            @Result(property = "target_user_id", column = "target_user_id"),
            @Result(property = "targetUser", column = "target_user_id", one = @One(select = "selectUser"))
    })
    List<DiscussCommentReplyExt> selectReplyByComment(@Param("commentId") Long commentId,
                                                      @Param("start") int start,
                                                      @Param("end") int end);

    @Select("select * from wc_user where user_id=#{userId}")
    WcUser selectUser(@Param("userId") Long userId);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcDiscussCommentSqlProvider.class, method = "countByExample")
    int countByExample(WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @DeleteProvider(type = WcDiscussCommentSqlProvider.class, method = "deleteByExample")
    int deleteByExample(WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @Delete({
            "delete from wc_discuss_comment",
            "where discuss_comment_id = #{discuss_comment_id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long discuss_comment_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @Insert({
            "insert into wc_discuss_comment (discuss_comment_id, discuss_id, ",
            "user_id, equip_id, ",
            "comment_time, is_anonymous, ",
            "content)",
            "values (#{discuss_comment_id,jdbcType=BIGINT}, #{discuss_id,jdbcType=BIGINT}, ",
            "#{user_id,jdbcType=BIGINT}, #{equip_id,jdbcType=BIGINT}, ",
            "#{comment_time,jdbcType=TIMESTAMP}, #{is_anonymous,jdbcType=INTEGER}, ",
            "#{content,jdbcType=LONGVARCHAR})"
    })
    int insert(WcDiscussComment record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @InsertProvider(type = WcDiscussCommentSqlProvider.class, method = "insertSelective")
    int insertSelective(WcDiscussComment record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcDiscussCommentSqlProvider.class, method = "selectByExampleWithBLOBs")
    @Results({
            @Result(column = "discuss_comment_id", property = "discuss_comment_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "comment_time", property = "comment_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_anonymous", property = "is_anonymous", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.LONGVARCHAR)
    })
    List<WcDiscussComment> selectByExampleWithBLOBs(WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcDiscussCommentSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "discuss_comment_id", property = "discuss_comment_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "comment_time", property = "comment_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_anonymous", property = "is_anonymous", jdbcType = JdbcType.INTEGER)
    })
    List<WcDiscussComment> selectByExample(WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @Select({
            "select",
            "discuss_comment_id, discuss_id, user_id, equip_id, comment_time, is_anonymous, ",
            "content",
            "from wc_discuss_comment",
            "where discuss_comment_id = #{discuss_comment_id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "discuss_comment_id", property = "discuss_comment_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "comment_time", property = "comment_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_anonymous", property = "is_anonymous", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.LONGVARCHAR)
    })
    WcDiscussComment selectByPrimaryKey(Long discuss_comment_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussCommentSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") WcDiscussComment record, @Param("example") WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussCommentSqlProvider.class, method = "updateByExampleWithBLOBs")
    int updateByExampleWithBLOBs(@Param("record") WcDiscussComment record, @Param("example") WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussCommentSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") WcDiscussComment record, @Param("example") WcDiscussCommentExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussCommentSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WcDiscussComment record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @Update({
            "update wc_discuss_comment",
            "set discuss_id = #{discuss_id,jdbcType=BIGINT},",
            "user_id = #{user_id,jdbcType=BIGINT},",
            "equip_id = #{equip_id,jdbcType=BIGINT},",
            "comment_time = #{comment_time,jdbcType=TIMESTAMP},",
            "is_anonymous = #{is_anonymous,jdbcType=INTEGER},",
            "content = #{content,jdbcType=LONGVARCHAR}",
            "where discuss_comment_id = #{discuss_comment_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKeyWithBLOBs(WcDiscussComment record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss_comment
     *
     * @mbggenerated
     */
    @Update({
            "update wc_discuss_comment",
            "set discuss_id = #{discuss_id,jdbcType=BIGINT},",
            "user_id = #{user_id,jdbcType=BIGINT},",
            "equip_id = #{equip_id,jdbcType=BIGINT},",
            "comment_time = #{comment_time,jdbcType=TIMESTAMP},",
            "is_anonymous = #{is_anonymous,jdbcType=INTEGER}",
            "where discuss_comment_id = #{discuss_comment_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(WcDiscussComment record);
}